package org.skydiveapp;

import org.skydiveapplibrary.repository.SettingsRepository;
import org.skydiveapplibrary.ui.InterfaceManager;
import org.skydiveapplibrary.R;
import org.skydiveapplibrary.data.Constants;
import org.skydiveapplibrary.data.FreefallProfileType;
import org.skydiveapplibrary.ui.FreefallProfileTypeSelectListAdapter;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class FreefallProfileTypeSelectListActivity extends BaseListActivity
{
	// data
	private boolean isDualDisplay;
	
	@Override
    public void onCreate(Bundle savedInstanceState)
    { 
		SettingsRepository settingsRepository = new SettingsRepository(this);
		if (settingsRepository.getUseLightTheme())
			super.setTheme(R.style.SkydiveAppTheme_Light_Transparent);
		
        super.onCreate(savedInstanceState);

    	isDualDisplay = this.findViewById(R.id.phoneLayout) == null;
    	
        // init view
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		
    	InterfaceManager.getInstance().setSubtitleText(getSupportActionBar(), 
				getResources().getString(R.string.select_freefall_profile_type_label));
    }
    
    @Override
    public void onResume()
    {
        super.onResume();
        
        // init listview
        FreefallProfileTypeSelectListAdapter adapter = new FreefallProfileTypeSelectListAdapter(this);
                setListAdapter(adapter);
                
        // set initial selection
        FreefallProfileType selectedType = getCurrentSelection();
        for (int index = 0; index < adapter.getCount(); index++)
        {
            getListView().setItemChecked(index,
                        adapter.getItems().get(index) == selectedType); 
        }
    }
    
    @Override
    protected void onListItemClick(ListView listView, View view, int indx, long id)
    {
        // get current selection
        FreefallProfileType selectedType = getCurrentSelection();
        // get new selection
        FreefallProfileTypeSelectListAdapter adapter = (FreefallProfileTypeSelectListAdapter)getListAdapter();
                FreefallProfileType newSelectedType = (FreefallProfileType)adapter.getItem(indx);
        
        // create new intent
        Intent intent =  new Intent();
        
        // if different than previous
        if (selectedType != newSelectedType)
        {
            intent.putExtra(Constants.EXTRA_FREEFALL_PROFILE_TYPE, newSelectedType.toString());
        }

        // set result, finish
        setResult(RESULT_OK, intent);
        close();
    }
    
    private FreefallProfileType getCurrentSelection()
    {
        String typeStr = getIntent().getStringExtra(Constants.EXTRA_FREEFALL_PROFILE_TYPE);
                return (typeStr != null) ? FreefallProfileType.valueOf(typeStr) : null;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.menu_back, menu);
        return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
    	menu.findItem(R.id.menu_back).setVisible(isDualDisplay);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int itemId = item.getItemId();
        
		if (itemId == R.id.menu_back)
		{
			close();
			return true;
		}
		else
		{
			return super.onOptionsItemSelected(item);
		}
    }
    
	@Override
    public void onBackPressed() {
		close();
    }
	
	private void close()
	{
		finish();
	}
} 